package com.shwread.httpsdk.http.base;

import android.content.Context;

import com.shwread.httpsdk.bean.FormFile;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.util.NetUtil;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.MultipartPostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpStatus;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map.Entry;

import logic.constants.Const;


/**
 * @author 刘章
 * @Title: AbstractHttpURLConnection.java
 * @Package com.surfingread.httpsdk.http.base
 * @Description: TODO(添加描述)
 * @date 2014年2月19日 下午7:34:59
 */
public abstract class AbstractHttpURLPostForFile
        extends
        AbstractHttpBase {
    private HashMap<String, String> parameter_map = new HashMap<String, String>();

    private HttpURLConnection httpConnection;
    private InputStream inStream;
    private DataOutputStream outStream;
    private String action;
    private File file;
    private FormFile formFile;

    public AbstractHttpURLPostForFile(Context mContext, String action, ActionListener actionListener) {
        super(mContext, action, actionListener);
        this.action = action;
    }

    protected abstract void getParameter(HashMap<String, String> parameter_map);

    protected abstract File getFile();

    public abstract void doResponseXml(String xml);

    public String getUrl() {
        return Const.SERVER_URL + "/" + action;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void doHttp() {
        try {
            HttpClient httpClient = new HttpClient();
            @SuppressWarnings("deprecation")
            MultipartPostMethod post = new MultipartPostMethod(getUrl());
            getParameter(parameter_map);
            if (parameter_map != null && !parameter_map.isEmpty()) {
                for (Entry<String, String> entry : parameter_map.entrySet()) {
                    post.addParameter(entry.getKey(), entry.getValue());
                }
            }
            file = getFile();
            post.addParameter("imageData", "temp.jpeg", file);
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

            int code = httpClient.executeMethod(post);
            println("code:" + code);

            if (code == HttpStatus.SC_OK) {
                doResponseXml(post.getResponseBodyAsString());
            } else {
                HTTPERROR(code, null);
            }
        } catch (Exception e) {
            HTTPERROR(NetUtil.NetError, e);
        } finally {
            DoDispose();
        }
    }

    public void DoDispose() {
        if (file != null) {
            file = null;
        }
        if (formFile != null) {
            formFile = null;
        }
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            inStream = null;
        }
        if (outStream != null) {
            try {
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            outStream = null;
        }
        if (httpConnection != null) {
            httpConnection.disconnect();
            httpConnection = null;
        }
        if (header_map != null) {
            header_map.clear();
            header_map = null;
        }
        if (listener != null) {
            listener = null;
        }
        dispose();
        context = null;
    }
}
